package io.renren.modules.seller.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.JsonMappingException;
import io.renren.modules.seller.entity.PageResult;
import io.renren.modules.seller.entity.Result;
import io.renren.modules.seller.entity.TypeTemplateEntity;
import io.renren.modules.seller.service.TypeTemplateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author Mark
 * @date 2020-02-16 11:30:48
 */
@Slf4j
@RestController
@RequestMapping("manage/template")
public class TypeTemplateController {

    @Autowired
    private TypeTemplateService typeTemplateService;

    /**
     * 返回全部列表
     *
     * @return list
     */
    @RequestMapping("/findAll")
    public List<TypeTemplateEntity> findAll() {
        return typeTemplateService.list();
    }

    /**
     * 返回全部列表
     *
     * @return PageResult
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestParam Map<String, Object> params) {
        return typeTemplateService.findPage(params);
    }

    /**
     * 查询+分页
     *
     * @return PageResult
     */
    @RequestMapping("/search")
    public PageResult search(@RequestBody TypeTemplateEntity entity, String page, String rows) {
        Map<String, Object> params = new HashMap<>(8);
        params.put("page", page);
        params.put("limit", rows);
        params.put("name", entity.getName());
        return typeTemplateService.findPage(params);
    }

    /**
     * 增加
     *
     * @return Result
     */
    @RequestMapping("/add")
    public Result add(@RequestBody String data) {
        try {
//            {"customAttributeItems":[{"text":"材质"},{"text":"产地"},{"text":"颜色"},{"text":"价格"}]
//,"name":"茶壶","brandIds":[{"id":2,"text":"华为"},{"id":8,"text":"魅族"}],"specIds":[{"id":26,"text":"尺码"}]}
            JSONObject json = JSON.parseObject(data);
            TypeTemplateEntity typeTemplate = new TypeTemplateEntity();
            typeTemplate.setName(json.getString("name"));
            typeTemplate.setBrandIds(json.getString("brandIds"));
            typeTemplate.setSpecIds(json.getString("specIds"));
            typeTemplate.setCustomAttributeItems(json.getString("customAttributeItems"));
            typeTemplateService.save(typeTemplate);
            return new Result(true, "增加成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return new Result(false, "增加失败");
        }
    }

    /**
     * 修改
     *
     * @return Result
     */
    @RequestMapping("/update")
    public Result update(@RequestBody String data) {
        try {
            JSONObject json = JSON.parseObject(data);
            TypeTemplateEntity typeTemplate = new TypeTemplateEntity();
            typeTemplate.setId(json.getLong("id"));
            typeTemplate.setName(json.getString("name"));
            typeTemplate.setBrandIds(json.getString("brandIds"));
            typeTemplate.setSpecIds(json.getString("specIds"));
            typeTemplate.setCustomAttributeItems(json.getString("customAttributeItems"));
            typeTemplateService.updateById(typeTemplate);
            return new Result(true, "修改成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return new Result(false, "修改失败");
        }
    }

    /**
     * 获取实体
     *
     * @return TypeTemplateEntity
     */
    @RequestMapping("/findOne")
    public TypeTemplateEntity findOne(Long id) {
        return typeTemplateService.getById(id);
    }

    /**
     * 批量删除
     *
     * @return Result
     */
    @RequestMapping("/delete")
    public Result delete(Long[] ids) {
        try {
            typeTemplateService.removeByIds(Arrays.asList(ids));
            return new Result(true, "删除成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return new Result(false, "删除失败");
        }
    }

    @RequestMapping("/findSpecList")
    public List<Map> findSpecList(Long id){
        return typeTemplateService.findSpecList(id);
    }

}
